package com.ss.stream.demo1;

import javax.naming.Name;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @Auther: ljy.s
 * @Date: 2022/9/12 - 09 - 12 - 20:10
 */
public class Demo01List {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");
        list.add("赵敏");
        list.add("张强");
        list.add("张三丰");

        list.stream()
                .filter(name -> name.startsWith("张"))
                .filter(Name -> Name.length() == 3)
                .forEach(name -> System.out.print(" " + name));

        System.out.println();

        List<String> listA = new ArrayList<>();
        for (String s : list) {
            if (s.startsWith("张")) {
                listA.add(s);
            }
        }

        // 对listA集合进行过滤，只要姓名长度为3的人，存储到一个新的集合中
        List<String> listB = new ArrayList<>();
        for (String s : listA) {
            if (s.length() == 3) {
                listB.add(s);
            }
        }

        // 遍历listB集合
        for (String s : listB) {
            System.out.print(" " + s);
        }

        System.out.println();

        // 使用Stream实现
        Stream<String> stream = list.stream();
        stream.filter(name -> name.startsWith("张"))// 这里的stream对象，和下面的stream对象不是同一个
                .filter(Name -> Name.length() == 3)
                .forEach(name -> System.out.print(" " + name));
        //stream.limit(1);// Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
    }
}
